/**
 * @function 文件处理api
 * @author anytao
 * @created 2016-07-18
 * @version 1.0
 */

'use strict'

import Base from './base.js';
import fs from 'fs';
import path from 'path';

export default class extends Base {

    /**
     * 文件上传
     */
    uploadAction() {

        let result = {
            statusCode: '200',
            message: '上传成功',
            filename: 'images/test.jpg'
        };

        var file = think.extend({}, this.file('file'));
        var filepath = file.path;

        //文件上传后，需要将文件移动到项目其他地方，否则会在请求结束时删除掉该文件
        var uploadPath = think.RESOURCE_PATH + '/static/upload';
        think.mkdir(uploadPath);
        var basename = path.basename(filepath);
        fs.renameSync(filepath, uploadPath + '/' + basename);

        file.path = uploadPath + '/' + basename;

        result.filename=basename;
        think.log(file);
        return this.json(result);
    }
}